import { EnumResult } from '@/assets/model/enums/EnumUtil';
import { AxiosPromise } from 'axios';
import { ElMessage, ElMessageBox } from 'element-plus';
import type { Action } from 'element-plus';
/**
 * 访问方法封装
 */
export default class RequestUtil {
	/**
	 * 通用结果处理
	 * @param func 返回结果
	 * @param _this 页面对象
	 * @returns 状态码
	 */
	public static OperateData(func: AxiosPromise<any>): Promise<EnumResult> {
		let methodFunc = new Promise<EnumResult>((reslove, reject) => {
			func
				.then(function (response: any) {
					if (response.data.code == EnumResult.success) {
						ElMessageBox.alert(response.data.msg, '操作提示', {
							confirmButtonText: '确定',
							type: 'success',
							callback: (action: Action) => {}
						});
						reslove(EnumResult.success);
					} else if (response.data.code == EnumResult.warning) {
						ElMessageBox.alert(response.data.msg, '操作提示', {
							confirmButtonText: '确定',
							type: 'warning',
							callback: (action: Action) => {}
						});
						reslove(EnumResult.warning);
					} else if (response.data.code == EnumResult.error) {
						ElMessageBox.alert(response.data.msg, '操作提示', {
							confirmButtonText: '确定',
							type: 'error',
							callback: (action: Action) => {}
						});
						reslove(EnumResult.error);
					}
				})
				.catch(function (error: any) {
					ElMessageBox.alert('系统错误', '操作提示', {
						confirmButtonText: '确定',
						type: 'error',
						callback: (action: Action) => {}
					});
					console.log(error);
					reslove(EnumResult.webError);
				});
		});
		return methodFunc;
	}
}
